package com.goods.controller.business;

import com.goods.business.service.BizInStockService;
import com.goods.common.model.system.User;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.JWTUtils;
import com.goods.common.vo.business.InStockDetailVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/business/inStock")
public class BizInStockController {

    @Autowired
    private BizInStockService bizInStockService;


    @GetMapping("/findInStockList")
    public ResponseBean findInStockList(@RequestParam Map<String,Object> paramMap){
        //查询入库单
        PageVO<InStockVO> inStockVOs = bizInStockService.findInStockList(paramMap);
        return ResponseBean.success(inStockVOs);
    }

    @PostMapping("/addIntoStock")
    public ResponseBean addIntoStock(@RequestBody InStockVO inStockVO, HttpServletRequest request) {
        String username = JWTUtils.getUsername(request.getHeader("Authorization"));
        inStockVO.setOperator(username);
        bizInStockService.saveStock(inStockVO);
        return ResponseBean.success();
    }

    @GetMapping("/detail/{id}")
    public ResponseBean getDetails(@PathVariable Long id, @RequestParam Integer pageNum){
        InStockDetailVO inStockDetailVO = bizInStockService.findInStockDetailById(id,pageNum);
        return ResponseBean.success(inStockDetailVO);
    }
    @PutMapping("/publish/{id}")
    public ResponseBean publishStock(@PathVariable Integer id){
        bizInStockService.updateStatus(id,0);//通过
        return ResponseBean.success();
    }
    @PutMapping("/remove/{id}")
    public ResponseBean removeInStock(@PathVariable Integer id){
        bizInStockService.updateStatus(id,1);//移除
        return ResponseBean.success();
    }
    @PutMapping("/back/{id}")
    public ResponseBean rebackInStock(@PathVariable Integer id){
        bizInStockService.updateStatus(id,0);//移除
        return ResponseBean.success();
    }

    @GetMapping("/delete/{id}")
    public ResponseBean deleteInStock(@PathVariable Integer id) {
        bizInStockService.deleteById(id);
        return ResponseBean.success();
    }

}
